package com.itheima.edu.info.heimaguanli.yonghu;

import com.itheima.edu.info.heimaguanli.luoji.luojiService;
import com.itheima.edu.info.heimaguanli.lei.XueShengLei;

import java.util.Arrays;
import java.util.Scanner;

public class YongHuController {
    Scanner sc = new Scanner(System.in);
    luojiService luojiService = new luojiService();

    public void kaishi() {
        Scanner sc = new Scanner(System.in);
        lo:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String jianru = sc.next();
            switch (jianru) {
                case "1":
//                    System.out.println("添加学生");
                    tianjia();
                    break;
                case "2":
//                    System.out.println("删除学生");
                    shanchu();
                    break;
                case "3":
//                    System.out.println("修改学生");
                    xiugai();
                    break;
                case "4":
//                    System.out.println("查看学生");
                    chakan();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统,再见");
                    break lo;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }

    }

    private void xiugai() {
        String jianru;
        while (true) {
            System.out.println("请输入需要修改的学生ID:");
             jianru = sc.next();
            boolean panduan = luojiService.panduan(jianru);
            if (!panduan) {
                System.out.println("您输入的id不存在,请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String xingming = sc.next();
        System.out.println("请输入学生年龄:");
        String lianling = sc.next();
        System.out.println("请输入学生生日:");
        String shengri = sc.next();

        XueShengLei xueShengLei = new XueShengLei(jianru, xingming, lianling, shengri);
        //输入要修改的学生ID,和修改的参数(封装好的东西)
        luojiService.chuanshu(jianru,xueShengLei);
        System.out.println("修改成功!");

    }


    public void shanchu() {
        String jianru;
        while (true) {
            System.out.println("请输入需要删除的学生ID:");
            jianru = sc.next();
            boolean panduan = luojiService.panduan(jianru);
            //返回T就存在这个ID,如果返回F,这个ID就不存在
            if (!panduan) {
                System.out.println("您输入的id不存在,请重新输入");
            } else {
                break;
            }

        }
        luojiService.shanchu(jianru);
        System.out.println("删除成功!");
    }

    public void chakan() {

        XueShengLei[] chakanhou = luojiService.chadiao();
        if (chakanhou == null) {
            System.out.println("查无信息,请添加后查询");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < chakanhou.length; i++) {
                XueShengLei shuzu = chakanhou[i];
                //用if把null过滤掉不然会把null也打印出来   就会空指针异常
                if (shuzu != null) {
                    System.out.println(shuzu.getId() + "\t" + shuzu.getXingming() + "\t" + shuzu.getLianling() + "\t" + shuzu.getShengri());
                }


            }
        }
    }

    public void tianjia() {
        //接收用户输入的指令传给luoji模块给予处理
        String id;
        //接收
        while (true) {
            System.out.println("请输入ID:");
            id = sc.next();
            //判断id是不是重复的,把逻辑板块new过来(new过一次了放到方法内疚可以都使用)把ID传给他让他传到数组板块去判断
            boolean panduanhou = luojiService.panduan(id);
            //返回接收如果是T就被占用了,F就结束循环继续录入
            if (panduanhou) {
                System.out.println("ID被已被占用,请重新输入");
            } else {
                break;
            }
        }

        System.out.println("请输入姓名:");
        String xingming = sc.next();
        System.out.println("请输入年龄:");
        String lianling = sc.next();
        System.out.println("请输入生日:");
        String shengri = sc.next();
        //把学生类new过来,打包封装
        XueShengLei xueshenglei = new XueShengLei(id, xingming, lianling, shengri);
        //一层一层传递,并不是封装好就直接传递和赋值到数组中去了
//        luojiService luojiService = new luojiService();节约内存位置上调
        boolean jieguo = luojiService.addxueshenglei(xueshenglei);
        //接收布尔返回值(上面的boolean jieguo就是接收的返回值)
        //if判断如果是true会进第一个,否则就进else
        if (jieguo) {
            System.out.println("添加成功!");
        } else {
            System.out.println("添加失败~");

        }
    }
}
